package generateXML;

import java.io.File;   
import java.util.Iterator;   

import org.apache.log4j.Logger;
import org.dom4j.Document;   
import org.dom4j.DocumentException;   
//import org.dom4j.DocumentHelper;   
import org.dom4j.Element;   
import org.dom4j.io.SAXReader;   

//import org.dom4j.io.XMLWriter; 
import myPDFParagraph.myParagraph;

import com.foxit.pdf.dao.PDF2PDF;

public class XmlParse {
	//  FP2PAddParagraph(long Proccessor, int iPage, int iParagraph, byte[] content);
	//  FP2PAddIndependLine(long Proccessor, int iPage, int iParagraph,int iLine,byte[] content);
	protected static final Logger logger = Logger.getLogger(XmlParse.class);
	
	@SuppressWarnings("unchecked")
	public void parserXml(String fileName, long Proccessor, int iPage) {   
		File inputXml=new File(fileName);   		
		SAXReader saxReader = new SAXReader();   
		try {   
			Document document = saxReader.read(inputXml);   
			Element employees = document.getRootElement();
			int nParagraph = -1;
			for(Iterator<myParagraph> i = employees.elementIterator(); i.hasNext();){   
				nParagraph ++;
				logger.debug("CurrParegraph: " + nParagraph);
				
				Element paragraphs = (Element) i.next();		// paragraphs
				int nParagraphIndex = Integer.parseInt(paragraphs.attributeValue("ID"));
				
				boolean bIsDepend = PDF2PDF.FP2PIsAllLineIsIndepend(Proccessor, iPage, nParagraph);
				if (!bIsDepend) {
					PDF2PDF.FP2PAddParagraph(Proccessor, iPage, nParagraphIndex, paragraphs.attributeValue("Content").getBytes());
				}
				else {
					for(Iterator<myParagraph> j = paragraphs.elementIterator(); j.hasNext();){   
						Element paragraph=(Element) j.next();   			// paragraph
						int nLineIndex = Integer.parseInt(paragraph.attributeValue("ID"));
						PDF2PDF.FP2PAddIndependLine(Proccessor, iPage, nParagraphIndex, nLineIndex, paragraph.attributeValue("Content").getBytes());
					}
				}
			}   
		} catch (DocumentException e) {   
			System.out.println(e.getMessage());   
			logger.debug("Exception: " + e.getMessage());
		}    
	}   
}
